﻿using aier.business.logic;
using aier.dataaccess.model;
using aier.dataaccess.dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace aier.wx.api.selfhost.Controllers
{
    /// <summary>
    /// 学校对像
    /// </summary>
    [RoutePrefix("api/School")]
    public class SchoolController : ApiController
    {
        /// <summary>
        /// 业务逻辑
        /// </summary>
        public SchoolBll SchoolInfoBll { get; set; }
        public CommonBll commonBll { get; set; }
        /// <summary>
        /// 工具类
        /// </summary>
        public ApiTool apiTool { get; set; }


        /// <summary>
        /// 获取所有的学校信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetAllSchool()
        {
            List<SchoolView> schools = SchoolInfoBll.QueryAllSchool();

            return Ok<List<SchoolView>>(schools);
        }


        ///// <summary>
        ///// 获取所有的学校等级选项集
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //public IHttpActionResult GetSchoolLevel()
        //{
        //    List<XrmOption> schoolLevels = SchoolInfoBll.QuerySchoolLevel().ToList<XrmOption>();

        //    return Ok<List<XrmOption>>(schoolLevels);
        //}

        /// <summary>
        /// 通过ID获取学校信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetSchoolById(string schoolId)
        {
            SchoolView SchoolView = SchoolInfoBll.QuerySchoolById(schoolId);

            return Ok<SchoolView>(SchoolView);
        }

        /// <summary>
        /// 通过地区获取学校信息
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IHttpActionResult GetSchoolByProviceCityCounty(string provice,string city,string area)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            var ownerId = userInfo.userid;
            string hospital = userInfo.siteid.ToString();
            List<SchoolView> SchoolViews = SchoolInfoBll.QuerySchoolByProviceCityCounty(provice, city, area, hospital);

            return Ok<List<SchoolView>>(SchoolViews);
        }

        /// <summary>
        /// 创建学校
        /// </summary>
        /// <param name="schoolInfo">需要创建的学校对像</param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public IHttpActionResult Put(School schoolInfo)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            schoolInfo.userid = userInfo.userid;
            return Ok(commonBll.SaveSchool(schoolInfo));
        }

        /// <summary>
        /// 修改学校
        /// </summary>
        /// <param name="schoolInfo">需要修改的对像</param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public IHttpActionResult Post(School schoolInfo )
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            schoolInfo.userid = userInfo.userid;
            return Ok(commonBll.SaveSchool(schoolInfo));
        }

        /// <summary>
        /// 根据不同查询条件查询学校对像,
        /// 查询条件为：省ID,市ID,区/县ID,层级,学校ID,学校名称,学段,医院ID
        /// 原程序 querySchool
        /// </summary>
        /// <param name="school">学校对像</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("GetSchool")]
        public IHttpActionResult GetSchool([FromUri]SchoolView school)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            //var ownerId = userInfo.userid;
            //school.new_hospital = new Guid(userInfo.siteid.ToString());
            var schools = SchoolInfoBll.QuerySchool(school);

            return Ok<IEnumerable<SchoolView>>(schools);
        }


        [HttpGet]
        [Authorize]
        [ActionName("QuerySchool")]
        public IHttpActionResult QuerySchool([FromUri]SchoolView school)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            var ownerId = userInfo.userid;
            school.new_hospital = new Guid(userInfo.siteid.ToString());
            var schools = SchoolInfoBll.QuerySchool(school);

            return Ok<IEnumerable<SchoolView>>(schools);
        }

        /// <summary>
        /// 根据不同查询统计学校对像数量,
        /// 查询条件为：省ID,市ID,区/县ID,街道ID,学校ID,学校名称
        /// </summary>
        /// <param name="school">学校对像</param>
        /// <returns></returns>
        [ActionName("GetSchoolCount")]
        public IHttpActionResult QuerySchoolCount([FromUri]SchoolView school)
        {
            int schools = SchoolInfoBll.QuerySchoolCount(school);

            return Ok<int>(schools);
        }

    }
}
